import router from '@/router'
import { imgApi } from '@/http/api'
import { downloadsPath } from '@/service/file'

// 附件，文件预览
export const usePreview = async (filePath: string) => {
  let link = router.resolve({
    path: '/FileEcho',
    query: {
      fileName: filePath
    }
  })
  window.open(link.href, "_blank")
}

// 附件,文件下载
export const useDownload = async (file1: object | any[]) => {
  let file = file1 instanceof Array ? file1[0] : { ...file1, originalfilename: file1['filename'] };
  let path = file1 instanceof Array ? file1.map(i => {
    return { ...i, originalfilename: i['filename'] }
  }) : [ file ];
  await downloadsPath({ ...file, path, biztype: '1', moduletype: '1'})
}

// 预览path 转换
export const useSetViewPath = (url: string): string  => {
  return (url || '').replace('https://127.0.0.1', imgApi)
}
